﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace RealEstateCenter
{
    public partial class RequestAssignment : System.Web.UI.Page
    {
        System.Data.SqlClient.SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            new login_check("agency");
            if (!IsPostBack)
            {
                BindUserDetails();
            }
        }
        protected void BindUserDetails()
        {
            //connection open
            con.Open();
            //sql command to execute query from database
            SqlCommand cmd = new SqlCommand("Select * from Estate where estate_agency_id='0'", con);
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            //Binding data to gridview
            GridView1.DataSourceID = null;
            GridView1.DataSource = ds;
            GridView1.DataBind();
            GridView1.DataSource = null;
            GridView1.DataSourceID = "SqlDataSource1";
            con.Close();
        }
        protected void lnkassign_Click(object sender, EventArgs e)
        {

            LinkButton lnkbtn = sender as LinkButton;
            //getting particular row linkbutton
            GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
            //getting userid of particular row
            int userid = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
            string username = gvrow.Cells[0].Text;
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO Assignment (estate_id, agency_id) VALUES (" + userid + ", "+Session["agency_id"]+")", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindUserDetails();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('Assignment Request Sent Successfully')", true);
            }
        }
    }
}